/* 
 * Copyright (c) 2009, Giora Kosoi giorak@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Giora Kosoi ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Giora Kosoi BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sparrow/handler.h>
#include <sparrow/errors.h>

#include <typeinfo>

Handler::Handler(Reactor *r)
    : r_(r)
{
}

Handler::~Handler()
{
}

void Handler::handle_input(int fd, uint64_t token)
{
    throw_error<std::runtime_error>(__FILE__, __LINE__, 
        "Handler [%s] is not registered for handling input",
        demangle(typeid(*this).name()).c_str());
}

void Handler::handle_output(int fd, uint64_t token)
{
    throw_error<std::runtime_error>(__FILE__, __LINE__, 
        "Handler [%s] is not registered for handling output",
        demangle(typeid(*this).name()).c_str());
}

void Handler::handle_error(int fd, uint64_t token)
{
    throw_error<std::runtime_error>(__FILE__, __LINE__, 
        "Handler [%s] is not registered for handling errors",
        demangle(typeid(*this).name()).c_str());
}

void Handler::handle_timeout(uint64_t token)
{
    throw_error<std::runtime_error>(__FILE__, __LINE__, 
        "Handler [%s] is not registered for handling timeouts",
        demangle(typeid(*this).name()).c_str());
}

void Handler::push(int port, VariantSet *v)
{
    (*get_output(port))(v);
}

void Handler::push(const std::string &port, VariantSet *v)
{
    (*get_output(port))(v);
}

void Handler::call_once()
{
    for(int i = 0; i < once_.array_size(); ++i)
    {
        (*get_once(i))(NULL);
    }
    once_.clear();
}

void Handler::add_output(MemberCallerInterface *caller, int port)
{
    if(output_.get(port, std::nothrow) != NULL)
    {
        throw_error<std::logic_error>(__FILE__, __LINE__, 
                "output port %d in Handler %s is already connected to %s",
                port, demangle(typeid(*this).name()).c_str(), demangle(typeid(*caller).name()).c_str());
    }
    output_.set(port, new VariantPtr(caller, &Handler::delete_handler, true));
}

void Handler::add_output(MemberCallerInterface *caller, const std::string &port)
{
    if(output_.get(port, std::nothrow) != NULL)
    {
        throw_error<std::logic_error>(__FILE__, __LINE__, 
                "output port %s in Handler %s is already connected to %s",
                port.c_str(), demangle(typeid(*this).name()).c_str(), demangle(typeid(*caller).name()).c_str());
    }
    output_.set(port, new VariantPtr(caller, &Handler::delete_handler, true));
}

void Handler::add_input(MemberCallerInterface *caller, int port)
{
    if(input_.get(port, std::nothrow) != NULL)
    {
        throw_error<std::logic_error>(__FILE__, __LINE__, 
                "input port %d in Handler %s is already connected to %s",
                port, demangle(typeid(*this).name()).c_str(), demangle(typeid(*caller).name()).c_str());
    }
    input_.set(port, new VariantPtr(caller, &Handler::delete_handler, true));
}

void Handler::add_input(MemberCallerInterface *caller, const std::string &port)
{
    if(input_.get(port, std::nothrow) != NULL)
    {
        throw_error<std::logic_error>(__FILE__, __LINE__, 
                "input port %s in Handler %s is already connected to %s",
                port.c_str(), demangle(typeid(*this).name()).c_str(), demangle(typeid(*caller).name()).c_str());
    }
    input_.set(port, new VariantPtr(caller, &Handler::delete_handler, true));
}

void Handler::add_once(MemberCallerInterface *caller)
{
   once_.insert(new VariantPtr(caller, &Handler::delete_handler, false));
}


MemberCallerInterface *Handler::get_input(int port)
{
    // don't crash
    return static_cast<MemberCallerInterface *>(input_.get(port)->ptr());
}

MemberCallerInterface *Handler::get_input(const std::string &port)
{
    // don't crash
    return static_cast<MemberCallerInterface *>(input_.get(port)->ptr());
}


MemberCallerInterface *Handler::get_output(int port)
{
    return static_cast<MemberCallerInterface *>(output_.get(port)->ptr());
}

MemberCallerInterface *Handler::get_output(const std::string &port)
{
    return static_cast<MemberCallerInterface *>(output_.get(port)->ptr());
}

MemberCallerInterface *Handler::get_once(int index)
{
    return static_cast<MemberCallerInterface *>(once_.get(index)->ptr());
}

Reactor *Handler::get_reactor(const std::nothrow_t &)
{
    return r_;
}

Reactor *Handler::get_reactor()
{
    if(r_ == NULL)
    {
        throw_error<std::logic_error>(__FILE__, __LINE__,
                "preventing NULL pointer access for Handler %s, "
                "if intentional call Reactor(const std::nothrow_t &)",
                demangle(typeid(*this).name()).c_str());

    }
    return r_;
}


void Handler::connect_output(
        Handler *source, int source_port, 
        Handler *dest, int dest_port)
{
    // verify connections are one to one
    source->add_output(dest->get_input(dest_port), source_port);
}

void Handler::connect_output(
        Handler *source, int source_port, 
        Handler *dest, const std::string &dest_port)
{
    source->add_output(dest->get_input(dest_port), source_port);
}

void Handler::connect_output(
        Handler *source, const std::string &source_port, 
        Handler *dest, int dest_port)
{
    source->add_output(dest->get_input(dest_port), source_port);
}

void Handler::connect_output(
        Handler *source, const std::string &source_port, 
        Handler *dest, const std::string &dest_port)
{
    source->add_output(dest->get_input(dest_port), source_port);
}


size_t Handler::num_outputs()
{
    return output_.array_size();
}

void Handler::delete_handler(void *p)
{
    delete static_cast<Handler *>(p);
}


